//
//  WLLEmoticonPageViewCell.swift
//  WLLWeibo
//
//  Created by 王乐乐 on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class WLLEmoticonPageViewCell: UICollectionViewCell {
    
    //定义一个属性(一维数组 8  20) 供外界调用
    var  emoticons : [WLLEmoticonModel]?{
        didSet{
           //先全部隐藏(cell 复用问题)
            for button in buttonArray {
                button.hidden  = true
            }
            //遍历 emoticons
            for (i,emoticonModel) in emoticons!.enumerate() {
//                print(emoticons?.count)
                //获取 button
                let button = buttonArray[i]
                //显示 button(cell 复用的问题)
                button.hidden = false
                //判断是图片表情还是 emoji 表情
                //是 emoji 表情要设置 title
                
                if emoticonModel.isEmoji {
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    
                    button.setTitle(code, forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                    
                }else{
                    //是表情键盘
                    button.setImage(UIImage.emoticonsImage(emoticonModel), forState: UIControlState.Normal)
                    
                    button.setTitle(nil, forState: UIControlState.Normal)
                    
                }
                
                
                
                
            }
            
            
        }
    }
    
   
    
    var indexPath : NSIndexPath?{
        
        didSet{
            //设置 text
//            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
            
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     //MARK: - 设置视图
    func setupUI() {
        
            addChildButtons()
        
        contentView.addSubview(deleteButton)
        
//        backgroundColor = RandomColor()
        //添加控件...如果这里没有调用会崩,因为数据源数组中没有数据, count 为零. didSet 里面的方法会越界
    
//       //观察到从第1组开始颜色循环.
//        contentView.addSubview(messageLabel)
//        //添加约束
//        messageLabel.snp_makeConstraints { (make) in
//            make.center.equalTo(contentView)
//        
//        }
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算 button 的宽和高
        let w  = (self.frame.width - 10)/CGFloat(WLLEmoticonsMaxCol)
        let h = (self.frame.height - 20)/CGFloat(WLLEmoticonsMaxRow)
        
        //设置20个按钮的 frame
        //遍历buttonArray
        for (index,button) in buttonArray.enumerate() {
            //设置 frame
            let col = CGFloat(index%WLLEmoticonsMaxCol)
            let row = CGFloat(index/WLLEmoticonsMaxCol)
            
            button.frame = CGRect(x: col*w + 5 , y: row*h, width: w, height: h)
            
        }
        
        //给删除按钮设置 frame
        deleteButton.frame = CGRect(x: KSCREENW - 5 - w, y: 2*h, width: w, height: h)
        
    }
    
    
    //定义一个可变的空数组
    var buttonArray : [UIButton] = [UIButton]()
    
    
    /**
     "创建20个按钮"
     */
    func addChildButtons() {
        //循环创建 button
        
        for _ in 0..<WLLEmoticonsMaxCount {
            
            let button = UIButton()
            //设置字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
//            //设置背景颜色
//            button.backgroundColor = RandomColor()
            //添加 button到数组
             buttonArray.append(button)
            //添加到视图
            contentView.addSubview(button)
     
        }
    }
    
    func deleteAction() {
        print("删除")
    }
    
    
    
     //MARK: - 懒加载控件
    private lazy var messageLabel : UILabel = UILabel(textColor:UIColor.redColor(), textFont: 35, maxWidth: KSCREENW)
    
    private lazy var deleteButton: UIButton = {
        let btn = UIButton()
        
        btn.addTarget(self , action: "deleteAction", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        return btn
    }()
    
}













